const { resolve } = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { HotModuleReplacementPlugin } = require("webpack");
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
  entry: "./webpack-basic-src/index.js",
  //   entry: {
  //       'index': './src/index.js',
  //       'a': './src/a.js',
  //       'b': './src/b.js',
  //   },
  output: {
    path: resolve(__dirname, "./webpack-basic-build"),
    // filename: "main.js",
    filename: "[name][chunkhash:6].js",
  },
  mode: "development",
  devtool: "eval-cheap-module-source-map",
  devServer: {
    port: 9090,
    open: true,
    proxy: {
      "/api": "http://localhost:3000",
    },
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.s[ac]ss$/i,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      {
        test: /\.less$/i,
        use: ["style-loader", "css-loader", "postcss-loader", "less-loader"],
      },
      // webpack4 方法
      // {
      //   test: /\.(png|jpe?g|gif|webp)$/,
      //   use: {
      //     // loader: "file-loader",
      //     loader: "url-loader",
      //     options: {
      //       name: "[name]_[chunkhash:6].[ext]",
      //       outputPath: "static/",
      //       limit: 5 * 1024, // 小于5kb的图片，会被转为base64的格式，放入bundle.js文件中
      //     },
      //   },
      // },
      // {
      //   test: /\.(eot|ttf|woff|woff2|svg)$/,
      //   use: {
      //     loader: "url-loader",
      //     options: {
      //       name: "[name]_[chunkhash:6].[ext]",
      //       outputPath: "iconfont/",
      //       limit: 1024,
      //     },
      //   },
      // },
      // webpack5 方法
      {
        test: /\.(png|jpe?g|gif|webp)$/,
        type: "asset/resource",
        generator: {
          filename: "[name][hash:6][ext]",
        },
      },
      {
        test: /\.(eot|ttf|woff|woff2|svg)$/,
        type: "asset/resource",
        generator: {
          filename: "[name][hash:6][ext]",
        },
      },
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
      {
        test: /\.vue$/,
        loader: "vue-loader",
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "./index.html",
      filename: "index.html",
    }),
    new HotModuleReplacementPlugin(),
    // 处理Vue单文件，确保引入这个插件
    new VueLoaderPlugin()
  ],
};
